#include "StdAfx.h"
#include "global_data_impl.h"
#include "global_data.h"
#include <interface/aot_im_define.h>

namespace aot{ namespace glb{

user_status_mgr_impl::user_status_mgr_impl()
{
	;
}

user_status_mgr_impl::~user_status_mgr_impl()
{
	;
}

bool 
user_status_mgr_impl::get_status_info(const char* user_id, aot::glb::iuser_status_change_info** out)
{
	if( user_id == NULL || user_id[0] == 0 )
	{
		assert(0);
		*out = new aot::glb::user_status_change_info_impl();
		return true;
	}

	auto_lock_type __mon(this->mutex_);

	map_type::iterator it = this->map_.find(user_id);
	if( it == this->map_.end() )
	{
		aot::glb::iuser_status_change_info* s = new aot::glb::user_status_change_info_impl();
		s->user_id->assign(user_id);
		*out = s;
		return true;
	}
	return it->second->clone(out);
}

void 
user_status_mgr_impl::set_status_info(const char* user_id, aot::glb::iuser_status_change_info* p)
{
	if( user_id == NULL || user_id[0] == 0 )
	{
		assert(0);
		return;
	}

	if( p->status == aot::e_status_offline )
	{
		p->version = 0;
		p->ims_id = 0;
	}

	auto_lock_type __mon(this->mutex_);
	map_type::iterator it = this->map_.find(user_id);
	if( it == this->map_.end() )
	{
		aot::glb::iuser_status_change_info* s = NULL;
		p->clone(&s);
		this->map_.insert(std::make_pair(user_id, s));
		return;
	}

	it->second->assign(p);
}

int 
user_status_mgr_impl::get_status(const char* user_id)
{
	if( user_id == NULL || user_id[0] == 0 )
	{
		assert(0);
		return aot::e_status_offline;
	}

	auto_lock_type __mon(this->mutex_);
	map_type::iterator it = this->map_.find(user_id);
	if( it == this->map_.end() )
	{
		return aot::e_status_offline;
	}
	return it->second->status;
}

void 
user_status_mgr_impl::set_status(const char* user_id, int status)
{
	if( user_id == NULL || user_id[0] == 0 )
	{
		assert(0);
		return;
	}

	auto_lock_type __mon(this->mutex_);
	map_type::iterator it = this->map_.find(user_id);
	if( it == this->map_.end() )
	{
		aot::glb::iuser_status_change_info* s = new aot::glb::user_status_change_info_impl();
		s->user_id->assign(user_id);
		s->status = status;
		this->map_.insert(std::make_pair(user_id, s));
		return;
	}

	it->second->old_status = it->second->status;
	it->second->status = status;
	if( status == aot::e_status_offline )
	{
		it->second->version = 0;
		it->second->ims_id = 0;
	}
}

void 
user_status_mgr_impl::remove(const char* user_id)
{
	if( user_id == NULL || user_id[0] == 0 )
	{
		assert(0);
		return;
	}

	auto_lock_type __mon(this->mutex_);
	map_type::iterator it = this->map_.find(user_id);
	if( it != this->map_.end() )
	{
		if( it->second )
		{
			it->second->destroy();
		}
		this->map_.erase(it);
	}
}

void 
user_status_mgr_impl::remove_all()
{
	auto_lock_type __mon(this->mutex_);

	map_type::iterator it = this->map_.begin();
	for( ; it != this->map_.end(); ++it )
	{
		if( it->second )
		{
			it->second->destroy();
		}
	}
	this->map_.clear();
}

void 
user_status_mgr_impl::set_all_user_status(int status)
{
	auto_lock_type __mon(this->mutex_);
	map_type::iterator it = this->map_.begin();
	for( ; it != this->map_.end(); ++it )
	{
		it->second->status = status;
		if( status == aot::e_status_offline )
		{
			it->second->version = 0;
			it->second->ims_id = 0;
		}
	}
}

bool 
user_status_mgr_impl::find(const char* user_id)
{
	if( user_id == NULL || user_id[0] == 0 )
	{
		assert(0);
		return false;
	}

	auto_lock_type __mon(this->mutex_);
	map_type::iterator it = this->map_.find(user_id);
	return it != this->map_.end();
}

bool 
user_status_mgr_impl::update(const char* user_id, aot::im::mini_user_info_t* m)
{
	if( user_id == NULL || user_id[0] == 0 )
	{
		assert(0);
		return false;
	}

	aot::glb::iuser_status_change_info* p = NULL;

	auto_lock_type __mon(this->mutex_);
	map_type::iterator it = this->map_.find(user_id);
	if( it == this->map_.end() )
	{
		p = new aot::glb::user_status_change_info_impl();
		p->user_id->assign(user_id);
		this->map_.insert(std::make_pair(user_id, p));
	}
	else
	{
		p = it->second;
	}

	if( m->info_version == 0 || (int)m->info_version >= p->version ) 
	{
		p->old_status = p->status;
		p->status = m->status;
		p->version = m->info_version;
		p->client_version = m->tata_version;
		p->inner_id = m->id;
		p->login_type = m->login_type;
		p->tata_public_ip = m->addr.ip;
		p->tata_public_port = m->addr.port;
		p->last_offline_tm = m->last_offline_tm;

		if( m->status == aot::e_status_offline )
		{
			p->ims_id = 0;
		}
		else
		{
			p->ims_id = m->ims_id;
		}
		return true;
	}
	return false;
}

int 
user_status_mgr_impl::get_ims_id(const char* user_id)
{
	if( user_id == NULL || user_id[0] == 0 )
	{
		assert(0);
		return 0;
	}

	auto_lock_type __mon(this->mutex_);
	map_type::iterator it = this->map_.find(user_id);
	if( it == this->map_.end() )
	{
		return 0;
	}
	return it->second->status == aot::e_status_offline ? 0 : it->second->ims_id;
}

aot::im::date_time_t 
user_status_mgr_impl::get_last_offline_tm(const char* user_id)
{
	if( user_id == NULL || user_id[0] == 0 )
	{
		assert(0);
		return aot::im::date_time_t();
	}

	auto_lock_type __mon(this->mutex_);
	map_type::iterator it = this->map_.find(user_id);
	if( it == this->map_.end() )
	{
		return aot::im::date_time_t();
	}
	return it->second->last_offline_tm;
}
///////////////////////////////////
bool online_notify_mgr_impl::init_from_db()
{
	aot::tt::iobj_auto_ptr<aot::data::iuser_op> user_op;

	if( !g_dll_obj->query_interface_from_data_op_dll(user_op, "iuser_op") )
		return false;

	auto_lock_type __tmp_lock(this->mutex_);
	aot::data::table_helper _table;

	this->notify_lst_.clear();
	if ( !user_op->online_notify_member_get(_table) )
	{
		return false;
	}

	std::string str_user_id;
	std::string str_user_name;
	for ( int i = 0; i < _table.get_rows_count(); i++ )
	{
		_table.set_row_index(i);
		str_user_id = _table.get_field_value_str("user_id");
		str_user_name = _table.get_field_value_str("nickname");
		insert_notify_member(str_user_id.c_str(), str_user_name.c_str());	
	}

	return true;
}

bool online_notify_mgr_impl::save_to_db()
{
	aot::tt::iobj_auto_ptr<aot::data::iuser_op> user_op;
	if( !g_dll_obj->query_interface_from_data_op_dll(user_op, "iuser_op") )
		return false;

	auto_lock_type __tmp_lock(this->mutex_);

	user_op->begin_transaction();
	user_op->online_notify_member_del_all();
	for (this->notify_lst_.goto_begin(); !this->notify_lst_.is_end(); this->notify_lst_.goto_next())
	{
		user_op->online_notify_member_insert(this->notify_lst_.get_key(), this->notify_lst_.get_value());
	}
	user_op->commit_transaction();
	return true;
}

bool online_notify_mgr_impl::del_all()
{
	aot::tt::iobj_auto_ptr<aot::data::iuser_op> user_op;

	if( !g_dll_obj->query_interface_from_data_op_dll(user_op, "iuser_op") )
		return false;

	auto_lock_type __mon(this->mutex_);
	user_op->online_notify_member_del_all();
	return true;
}

bool online_notify_mgr_impl::insert_notify_member( const char* user_id, const char* user_name, int no_use /*= 0*/ )
{
	if ( user_id == NULL || user_id[0] == 0 )
	{
		return false;
	}

	auto_lock_type __mon(this->mutex_);
	return this->notify_lst_.insert(user_id, user_name);
}

void online_notify_mgr_impl::del_notify_member( const char* user_id )
{
	auto_lock_type __mon(this->mutex_);
	this->notify_lst_.erase(user_id);

}

bool online_notify_mgr_impl::find( const char* user_id )
{
	auto_lock_type __mon(this->mutex_);
	return this->notify_lst_.find(user_id) ;
}

aot::tt::istr2str_map* 
online_notify_mgr_impl::get_all_notify_member()
{
	auto_lock_type __mon(this->mutex_);
	return &this->notify_lst_;
}
///////////////////////////////////

bool
tata_op_impl::create_user_mgr(aot::glb::iuser_mgr** out)
{
    return g_dll_obj->query_interface_from_global_data_dll((void**)out, "iuser_mgr");    
}

bool
tata_op_impl::create_cfg_mgr(aot::glb::icfg_mgr** out)
{
    return g_dll_obj->query_interface_from_global_data_dll((void**)out, "icfg_mgr");
}

bool 
tata_op_impl::create_user_lst_mgr( aot::glb::iuser_lst_mgr** out )
{
	return g_dll_obj->query_interface_from_global_data_dll((void**)out, "iuser_lst_mgr");
}

bool 
tata_op_impl::create_user_auth_mgr( aot::glb::iuser_auth_mgr** out )
{
	return g_dll_obj->query_interface_from_global_data_dll((void**)out, "iuser_auth_mgr");
}

bool
tata_op_impl::create_buddy_mgr(aot::glb::ibuddy_mgr** out)
{
    return g_dll_obj->query_interface_from_global_data_dll((void**)out, "ibuddy_mgr");
}

bool
tata_op_impl::create_online_notify_mgr(aot::glb::ionline_notify_mgr** out)
{
	return g_dll_obj->query_interface_from_global_data_dll((void**)out, "ionline_notify_mgr");
}


bool
tata_op_impl::create_global_res_string_mgr(aot::glb::iglobal_res_string_mgr** out)
{
    return g_dll_obj->query_interface_from_global_data_dll((void**)out, "iglobal_res_string_mgr");
}

bool 
tata_op_impl::create_user_status_mgr(aot::glb::iuser_status_mgr** out)
{
	return g_dll_obj->query_interface_from_global_data_dll((void**)out, "iuser_status_mgr");
}

bool 
tata_op_impl::create_user_status_change_info(aot::glb::iuser_status_change_info** out)
{
    return g_dll_obj->query_interface_by_iid((void**)out, aot::glb::iuser_status_change_info::iid);
}

bool 
tata_op_impl::create_buddy_group_info(aot::glb::ibuddy_group_info** out)
{
    return g_dll_obj->query_interface_by_iid((void**)out, aot::glb::ibuddy_group_info::iid);
}

bool 
tata_op_impl::create_buddy_group_mgr(aot::glb::ibuddy_group_mgr** out)
{
    return g_dll_obj->query_interface_from_global_data_dll((void**)out, "ibuddy_group_mgr");
}

bool 
tata_op_impl::create_tribe_member_info(aot::glb::itribe_member_info** out)
{
    return g_dll_obj->query_interface_from_global_data_dll((void**)out, "itribe_member_info");
}

bool 
tata_op_impl::create_tribe_info(aot::glb::itribe_info** out)
{
    return g_dll_obj->query_interface_from_global_data_dll((void**)out, "itribe_info");
}

bool 
tata_op_impl::create_tribe_mini_info(aot::glb::itribe_mini_info** out)
{
    return g_dll_obj->query_interface_from_global_data_dll((void**)out, "itribe_mini_info");
}

bool 
tata_op_impl::create_tribe_mgr(aot::glb::itribe_mgr** out)
{
    return g_dll_obj->query_interface_from_global_data_dll((void**)out, "itribe_mgr");
}

bool 
tata_op_impl::create_employee_info(aot::glb::iemployee_info** out)
{
    return g_dll_obj->query_interface_by_iid((void**)out, aot::glb::iemployee_info::iid);
}

bool 
tata_op_impl::create_enterprise_info(aot::glb::ienterprise_info** out)
{
    return g_dll_obj->query_interface_by_iid((void**)out, aot::glb::ienterprise_info::iid);
}

bool 
tata_op_impl::create_enterprise(aot::glb::ienterprise** out)
{
    return g_dll_obj->query_interface_from_global_data_dll((void**)out, "ienterprise");
}

bool 
tata_op_impl::create_recent_chat_mgr(aot::glb::irecent_chat_mgr** out)
{
    return g_dll_obj->query_interface_from_global_data_dll((void**)out, "irecent_chat_mgr");
}

bool
tata_op_impl::create_sys_cfg_db(aot::data::isqlite3_db** out)
{
    return g_dll_obj->query_interface_from_db_dll((void**)out, "isys_cfg");
}

bool
tata_op_impl::create_user_db(aot::data::isqlite3_db** out)
{
    return g_dll_obj->query_interface_from_db_dll((void**)out, "isqlite3_db");
}

bool
tata_op_impl::create_call_record_info(aot::data::icall_record_info** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "icall_record_info");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::icall_record_info::iid);
}

bool
tata_op_impl::create_call_record_op(aot::data::icall_record_op** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "icall_record_op");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::icall_record_op::iid);
}

bool
tata_op_impl::create_chat_log_info(aot::data::ichat_log_info** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "ichat_log_info");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::ichat_log_info::iid);
}

bool
tata_op_impl::create_chat_log_op(aot::data::ichat_log_op** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "ichat_log_op");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::ichat_log_op::iid);
}

bool 
tata_op_impl::create_sms_info(aot::data::isms_info** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "isms_info");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::isms_info::iid);
}

bool 
tata_op_impl::create_sms_group_info(aot::data::isms_group_info** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "isms_group_info");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::isms_group_info::iid);
}

bool 
tata_op_impl::create_sms_op(aot::data::isms_op** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "isms_op");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::isms_op::iid);
}

bool
tata_op_impl::create_user_info(aot::data::iuser_info** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "iuser_info");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::iuser_info::iid);
}

bool
tata_op_impl::create_user_op(aot::data::iuser_op** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "iuser_op");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::iuser_op::iid);
}

bool
tata_op_impl::create_clan_info(aot::data::iclan_info** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "iclan_info");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::iclan_info::iid);
}

bool
tata_op_impl::create_clan_op(aot::data::iclan_op** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "iclan_op");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::iclan_op::iid);
}

bool
tata_op_impl::create_contact_info(aot::data::icontact_info** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "icontact_info");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::icontact_info::iid);
}

bool
tata_op_impl::create_contact_group_info(aot::data::icontact_group_info** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "icontact_group_info");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::icontact_group_info::iid);
}

bool
tata_op_impl::create_contact_op(aot::data::icontact_op** out)
{
   // return g_dll_obj->query_interface_from_data_op_dll((void**)out, "icontact_op");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::icontact_op::iid);
}

bool
tata_op_impl::create_chat_reply_op(aot::data::ichat_reply_op** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "ichat_reply_op");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::ichat_reply_op::iid);
}

bool
tata_op_impl::create_mail_account_info(aot::data::imail_account_info** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "imail_account_info");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::imail_account_info::iid);
}

bool
tata_op_impl::create_mail_info(aot::data::imail_info** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "imail_info");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::imail_info::iid);
}

bool
tata_op_impl::create_mail_op(aot::data::imail_op** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "imail_op");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::imail_op::iid);
}

bool
tata_op_impl::create_white_name_op(aot::data::iwhite_name_op** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "iwhite_name_op");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::iwhite_name_op::iid);
}

bool
tata_op_impl::create_multi_chat_session_info(aot::data::imulti_chat_session_info** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "imulti_chat_session_info");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::imulti_chat_session_info::iid);
}

bool
tata_op_impl::create_multi_chat_session_op(aot::data::imulti_chat_session_op** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "imulti_chat_session_op");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::imulti_chat_session_op::iid);
}

bool 
tata_op_impl::create_login_info(aot::data::ilogin_info** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "ilogin_info");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::ilogin_info::iid);
}

bool 
tata_op_impl::create_login_info_op(aot::data::ilogin_info_op** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "ilogin_info_op");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::ilogin_info_op::iid);
}

bool 
tata_op_impl::create_transfile_info(aot::data::itransfile_info** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "itransfile_info");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::itransfile_info::iid);
}

bool 
tata_op_impl::create_transfile_op(aot::data::itransfile_op** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "itransfile_op");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::itransfile_op::iid);
}

bool 
tata_op_impl::create_shedule_info(aot::data::ishedule_info** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "ishedule_info");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::ishedule_info::iid);
}

bool 
tata_op_impl::create_shedule_op(aot::data::ishedule_op** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "ishedule_op");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::ishedule_op::iid);
}

bool 
tata_op_impl::create_recent_chat_info(aot::data::irecent_chat_info** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "irecent_chat_info");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::irecent_chat_info::iid);
}

bool 
tata_op_impl::create_recent_chat_op(aot::data::irecent_chat_op** out)
{
    //return g_dll_obj->query_interface_from_data_op_dll((void**)out, "irecent_chat_op");
    return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::irecent_chat_op::iid);
}

bool tata_op_impl::create_recv_file_info( aot::data::irecv_file_info** out )
{
	return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::irecv_file_info::iid);

}

bool tata_op_impl::create_recv_file_record_op( aot::data::irecv_file_record_op** out )
{
	return g_dll_obj->query_interface_by_iid_from_data_op_dll((void**)out, aot::data::irecv_file_record_op::iid);

}

}} /* end namespace aot/glb*/